<?php

namespace App\Console\Commands;

use App\Models\Staff;
use Carbon\Carbon;
use Illuminate\Console\Command;

class CalcWorkYears extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'calc:work-years';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'calc work year by staff enroll date';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $staffs = Staff::query()->where('status',1)->get();
        foreach ($staffs as $staff) {
            if($staff->date_enroll){
                $date = Carbon::parse($staff->date_enroll);
                $work_years = Carbon::now()->diffInYears($date);
                $staff->work_years = $work_years;
                $staff->save();
            }
        }
    }
}
